class Solution {
public:
    bool IsNarrowWindow(const unordered_map<char, int> &charNums, 
        unordered_map<char, int> &window) {
        // 循环遍历字符数量hashmap，查看窗口中是否包含了全部的字符
        for (auto iter = charNums.cbegin(); iter != charNums.cend(); ++iter) {
            char c = iter->first;
            int v = iter->second;
            if (window[c] < v) {
                return false;
            }
        }
        return true;
    }
    string minWindow(string s, string t) {
        unordered_map<char, int> charNums, window;
        int minSize = INT_MAX;
        int l;
        for (int i = 0; i < t.size(); ++i) {
            charNums[t[i]]++;
        }
        for (int right = 0, left = 0; right < s.size(); right++) {
            if (charNums.find(s[right]) != charNums.end()) {
                 window[s[right]]++;
            }
           
            while (IsNarrowWindow(charNums, window) && left <= right) {
                if (right - left + 1 <= minSize) {
                    minSize = right - left + 1;
                    l = left;
                }
                if (charNums.find(s[left]) != charNums.end()) {
                    window[s[left]]--;
                }
                left++;
            }
        }
        return minSize == INT_MAX ? "" : s.substr(l, minSize);
    }
};